﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net;
using Ext.Net.Utilities;
using MemberMgr.Data;
using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.ManagerObjects;
using System.Data;

namespace MemberMgr.Website
{
    public partial class Notes : Common.TabPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.noteStore.DataSource = this.GetDataTable();
                this.noteStore.DataBind();
            }
        }

        private DataTable GetDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn(){ ColumnName="ID",DataType=typeof(int)},
                new DataColumn(){ ColumnName="NOTETOPIC",DataType=typeof(string)},
                new DataColumn(){ ColumnName="NOTECONTENT",DataType=typeof(string)},
                new DataColumn(){ ColumnName="NOTETIME",DataType=typeof(DateTime)},
                new DataColumn(){ ColumnName="NOTEUSER",DataType=typeof(string)}
            });

            ItNoteManager tnoteMgr = Common.CommonUtil.managerFactory.GettNoteManager();

            IList<tNote> list = tnoteMgr.GetAll().OrderBy(p => p.Notetime).ToList();

            if (list != null)
            {
                foreach (tNote item in list)
                {
                    string username = string.Empty;

                    ItUserManager userMgr = Common.CommonUtil.managerFactory.GettUserManager();

                    tUser user = userMgr.GetById(item.Noteuser);

                    if (user != null)
                    {
                        username = user.Username;
                    }

                    dt.Rows.Add(new object[] { 
                        item.Id,
                        item.Notetopic,
                        item.Notecontent,
                        item.Notetime,
                        username
                    });
                }
            }

            return dt;
        }

        protected void noteStore_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
        }

        protected void noteStore_Submit(object sender, StoreSubmitDataEventArgs e)
        {
        }
    }
}
